package com.espeaker.sdk.api;

import org.json.JSONObject;

import com.espeaker.sdk.ESpeakerParameters;
import com.espeaker.sdk.NetException;
import com.espeaker.sdk.net.HttpManager;
import com.espeaker.sdk.utils.TimeUtil;
import com.espeaker.sdk.utils.Utility;
public class MiaoApi {
	private static final String LOG_TAG = MiaoApi.class.getSimpleName();

	protected static int TIME_DIFF = 0;

	public static final String MD5_BASE = "8d70e0d1acb06b4648c7aa8927509660";
	
	public static boolean IsDebug=false;
	
	//接口列表
	//登录
	protected static final String USER_LOGIN="login";
	//修改密码
	protected static final String USER_CHANGE_PASS="reset-password";
	//获取工资和批改条数
	protected static final String REVISE_GET_SALARY="salary";
	//拉取订单
	protected static final String REVISE_PULL="pull";
	//抢单
	protected static final String REVISE_GRAB="grab";
	//提交题目
	protected static final String REVISE_GETQuesiont="get-timu";
	//提交批改
	protected static final String REVISE_SUBMIT="submit";
	//未完成
	protected static final String REVISE_UNFINISHED="pull-unfinished";
	//打回
	protected static final String REVISE_REJECT="reject";
	//版本更新
	protected static final String GET_VERSION="app-update";
	//抢单
	protected static final String REVISE_FEEDBACK="feedback";
	// Http Post 方法
	public static final String HTTPMETHOD_POST = "POST";
	// Http Get 方法
	public static final String HTTPMETHOD_GET = "GET";
	/**
	 * source 消息来源
	 */
	public static enum SOURCE {

		/**
		 * android
		 */
		android("android");

		public final String value;

		SOURCE(String value) {
			this.value = value;
		}
	}
	public static String GetBaseURL()
	{
		if(IsDebug)
		{
			return "http://dev.miao.yishuodian.com/";
		}else
		{
			return "http://miao.yishuodian.com/";
		}
	}
	/*
	 * 获取和服务器时间差
	 */
	public static int getTimeDiff() {
		int mRes=0;
		String url = MiaoApi.GetBaseURL() + "now";
		ESpeakerParameters params = new ESpeakerParameters();
		try {
			String rlt = HttpManager.openUrl(url, HTTPMETHOD_GET, params, null);
			System.out.println(rlt);
			int serverTime = Integer.parseInt(rlt);
			mRes = TimeUtil.getCurrentTimeDiff(serverTime);
		} catch (NetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mRes;
	}
	/*
	 * 获取校验过的时间戳
	 */
	public static int getSeed() {
		int mRes=0;
		if (TIME_DIFF == 0) {
			TIME_DIFF=getTimeDiff();
		}
		mRes=TimeUtil.getCheckTime(TIME_DIFF);
		return mRes;
	}
	/*
	 * 获取Token
	 */
	public static String getToken()
	{
		String mRes="";
		mRes=getSeed()+MD5_BASE;
		mRes=Utility.md5(mRes);
		return mRes;
	}
	/*
	 * 获取签名 sign
	 */
	public static String getSign(JSONObject json) {
		String mRes = "";
		mRes = json + MD5_BASE;
		mRes = Utility.md5(mRes);
		return mRes;
	}
	/*
	 * 网络数据请求
	 */
	protected String request(final String url, final String api_method,
			final ESpeakerParameters params) {
		String rlt = "";
		
		JSONObject json = new JSONObject();
		for (int i = 0; i < params.size(); i++) {
				try {
					if(params.isSign(params.getKey(i))){
						json.put(params.getKey(i), params.getValue(i));
					}
				} catch (Exception er) {
			}
		}
		
		String newUrl = url + "api/teacher/1/" + api_method + "?seed="
				+ getSeed() + "&token=" + getToken() + "&source="
				+ SOURCE.android + "&sign=" + getSign(json);
		
		for (int i = 0; i < params.size(); i++) {
			try {
				if(!params.isSign(params.getKey(i))){
					json.put(params.getKey(i), params.getValue(i));
				}
			} catch (Exception er) {
		}
	}
		try {
			rlt = HttpManager.openPostUrl(newUrl,json);
			System.out.println("<================================rlt======"+rlt);
		} catch (NetException er) {
		}
		return rlt;
	}
	
}
